<div class="py-2">
    <article class="markdown-body">
        <h1><a id="user-content-静态文件服务配置" class="anchor" aria-hidden="true" href="#静态文件服务配置">
                <svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16"
                     aria-hidden="true">
                    <path fill-rule="evenodd"
                          d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
                </svg>
            </a>静态文件服务配置
        </h1>
        <p>默认情况下，<code>gf</code>框架已开启了静态文件服务功能，但是需要开发者配置<strong>静态文件目录</strong>才能提供服务。</p>
        <p>静态文件服务涉及到的常用配置方法如下：</p>
        <div class="highlight highlight-source-go"><pre><span class="pl-c">// 设置http server参数 - ServerRoot</span>
<span class="pl-k">func</span> (<span class="pl-s1">s</span> <span class="pl-c1">*</span><span
                        class="pl-smi">Server</span>) <span class="pl-en">SetServerRoot</span>(<span
                        class="pl-s1">root</span> <span class="pl-smi">string</span>)

<span class="pl-c">// 添加静态文件搜索目录，必须给定目录的绝对路径</span>
<span class="pl-k">func</span> (<span class="pl-s1">s</span> <span class="pl-c1">*</span><span
                        class="pl-smi">Server</span>) <span class="pl-en">AddSearchPath</span>(<span
                        class="pl-s1">path</span> <span class="pl-smi">string</span>)

<span class="pl-c">// 设置http server参数 - IndexFiles，默认展示文件，如：index.html, index.htm</span>
<span class="pl-k">func</span> (<span class="pl-s1">s</span> <span class="pl-c1">*</span><span
                        class="pl-smi">Server</span>) <span class="pl-en">SetIndexFiles</span>(<span
                        class="pl-s1">index</span> []<span class="pl-smi">string</span>)

<span class="pl-c">// 是否允许展示访问目录的文件列表</span>
<span class="pl-k">func</span> (<span class="pl-s1">s</span> <span class="pl-c1">*</span><span
                        class="pl-smi">Server</span>) <span class="pl-en">SetIndexFolder</span>(<span class="pl-s1">enabled</span> <span
                        class="pl-smi">bool</span>)

<span class="pl-c">// 添加URI与静态目录的映射</span>
<span class="pl-k">func</span> (<span class="pl-s1">s</span> <span class="pl-c1">*</span><span
                        class="pl-smi">Server</span>) <span class="pl-en">AddStaticPath</span>(<span class="pl-s1">prefix</span> <span
                        class="pl-smi">string</span>, <span class="pl-s1">path</span> <span class="pl-smi">string</span>)

<span class="pl-c">// 静态文件服务总开关：是否开启/关闭静态文件服务</span>
<span class="pl-k">func</span> (<span class="pl-s1">s</span> <span class="pl-c1">*</span><span
                        class="pl-smi">Server</span>) <span class="pl-en">SetFileServerEnabled</span>(<span
                        class="pl-s1">enabled</span> <span class="pl-smi">bool</span>)

<span class="pl-c">// 设置URI重写规则</span>
<span class="pl-k">func</span> (<span class="pl-s1">s</span> <span class="pl-c1">*</span><span
                        class="pl-smi">Server</span>) <span class="pl-en">SetRewrite</span>(<span
                        class="pl-s1">uri</span> <span class="pl-smi">string</span>, <span class="pl-s1">rewrite</span> <span
                        class="pl-smi">string</span>)

<span class="pl-c">// 设置URI重写规则（批量）</span>
<span class="pl-k">func</span> (<span class="pl-s1">s</span> <span class="pl-c1">*</span><span
                        class="pl-smi">Server</span>) <span class="pl-en">SetRewriteMap</span>(<span class="pl-s1">rewrites</span> <span
                        class="pl-k">map</span>[<span class="pl-smi">string</span>]<span class="pl-smi">string</span>) </pre>
        </div>
        <p>其中，</p>
        <ol>
            <li><code>IndexFiles</code>为当访问目录时默认检索的文件名称列表（按照slice先后顺序进行检索），当检索的文件存在时则返回文件内容，否则展示目录列表(<code>SetIndexFolder</code>为<code>true</code>时)，默认的<code>IndexFiles</code>为：<code>index.html,
                    index.htm</code>；
            </li>
            <li><code>SetIndexFolder</code>为设置是否在用户访问文件目录，且没有在目录下检索到<code>IndexFiles</code>时，则展示目录下的文件列表，默认为关闭；</li>
            <li><code>SetServerRoot</code>为设置默认提供服务的静态文件目录，该目录会被自动添加到<code>SearchPath</code>中的第一个搜索路径；</li>
            <li><code>AddSearchPath</code>为添加静态文件检索目录，可以有多个，按照文件目录添加的先后顺序执行优先级检索；</li>
            <li><code>AddStaticPath</code>为添加URI与目录路径的映射关系，可以自定义静态文件目录的访问URI规则；</li>
            <li><code>SetRewrite</code>/<code>SetRewriteMap</code>为重写规则设置（类似于<code>nginx</code>的<code>rewrite</code>），严格上来讲不仅仅是静态文件服务，当然也支持动态的路由注册的<code>rewrite</code>；
            </li>
        </ol>
        <blockquote>
            <p>Tips: 设置静态文件服务的目录路径时，可以使用绝对路径，也可以使用相对路径，例如设置当前运行目录提供静态文件服务可以使用<code>SetServerRoot(".")</code>。</p>
        </blockquote>
        <blockquote>
            <p>开发者可以设置多个文件目录来提供静态文件服务，并且可以设置目录及URI的优先级，但是一旦通过<code>SetFileServerEnabled</code>关闭了静态服务，所有静态文件/目录的访问都将失效。
            </p>
        </blockquote>
        <h1><a id="user-content-示例1-基本使用" class="anchor" aria-hidden="true" href="#示例1-基本使用">
                <svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16"
                     aria-hidden="true">
                    <path fill-rule="evenodd"
                          d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
                </svg>
            </a>示例1， 基本使用
        </h1>
        <div class="highlight highlight-source-go"><pre><span class="pl-k">package</span> main

<span class="pl-k">import</span> <span class="pl-s">"github.com/gogf/gf/frame/g"</span>

<span class="pl-c">// 静态文件服务器基本使用</span>
<span class="pl-k">func</span> <span class="pl-en">main</span>() {
    <span class="pl-s1">s</span> <span class="pl-c1">:=</span> <span class="pl-s1">g</span>.<span
                        class="pl-en">Server</span>()
    <span class="pl-s1">s</span>.<span class="pl-en">SetIndexFolder</span>(<span class="pl-c1">true</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">SetServerRoot</span>(<span class="pl-s">"/Users/john/Temp"</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">AddSearchPath</span>(<span
                        class="pl-s">"/Users/john/Documents"</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">SetPort</span>(<span class="pl-c1">8199</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">Run</span>()
}</pre>
        </div>
        <h1><a id="user-content-示例2静态目录映射" class="anchor" aria-hidden="true" href="#示例2静态目录映射">
                <svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16"
                     aria-hidden="true">
                    <path fill-rule="evenodd"
                          d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
                </svg>
            </a>示例2，静态目录映射
        </h1>
        <div class="highlight highlight-source-go"><pre><span class="pl-k">package</span> main

<span class="pl-k">import</span> <span class="pl-s">"github.com/gogf/gf/frame/g"</span>

<span class="pl-c">// 静态文件服务器，支持自定义静态目录映射</span>
<span class="pl-k">func</span> <span class="pl-en">main</span>() {
    <span class="pl-s1">s</span> <span class="pl-c1">:=</span> <span class="pl-s1">g</span>.<span
                        class="pl-en">Server</span>()
    <span class="pl-s1">s</span>.<span class="pl-en">SetIndexFolder</span>(<span class="pl-c1">true</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">SetServerRoot</span>(<span class="pl-s">"/Users/john/Temp"</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">AddSearchPath</span>(<span
                        class="pl-s">"/Users/john/Documents"</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">AddStaticPath</span>(<span class="pl-s">"/my-doc"</span>, <span
                        class="pl-s">"/Users/john/Documents"</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">SetPort</span>(<span class="pl-c1">8199</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">Run</span>()
}</pre>
        </div>
        <h1><a id="user-content-示例3静态目录映射优先级控制" class="anchor" aria-hidden="true" href="#示例3静态目录映射优先级控制">
                <svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16"
                     aria-hidden="true">
                    <path fill-rule="evenodd"
                          d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
                </svg>
            </a>示例3，静态目录映射，优先级控制
        </h1>
        <p>静态目录映射的优先级按照绑定的<code>URI</code>精准度进行控制，绑定的URI越精准（深度优先匹配），那么优先级越高。</p>
        <div class="highlight highlight-source-go"><pre><span class="pl-k">package</span> main

<span class="pl-k">import</span> <span class="pl-s">"github.com/gogf/gf/frame/g"</span>

<span class="pl-c">// 静态文件服务器，支持自定义静态目录映射</span>
<span class="pl-k">func</span> <span class="pl-en">main</span>() {
    <span class="pl-s1">s</span> <span class="pl-c1">:=</span> <span class="pl-s1">g</span>.<span
                        class="pl-en">Server</span>()
    <span class="pl-s1">s</span>.<span class="pl-en">SetIndexFolder</span>(<span class="pl-c1">true</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">SetServerRoot</span>(<span class="pl-s">"/Users/john/Temp"</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">AddSearchPath</span>(<span
                        class="pl-s">"/Users/john/Documents"</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">AddStaticPath</span>(<span class="pl-s">"/my-doc"</span>, <span
                        class="pl-s">"/Users/john/Documents"</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">AddStaticPath</span>(<span
                        class="pl-s">"/my-doc/test"</span>, <span class="pl-s">"/Users/john/Temp"</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">SetPort</span>(<span class="pl-c1">8199</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">Run</span>()
}</pre>
        </div>
        <p>其中，访问<code>/my-doc/test</code>的优先级会比<code>/my-doc</code>高，因此假如<code>/Users/john/Documents</code>目录下存在<code>test</code>目录（与自定义的<code>/my-doc/test</code>冲突），将会无法被访问到。
        </p>
        <h1><a id="user-content-示例4uri重写" class="anchor" aria-hidden="true" href="#示例4uri重写">
                <svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16"
                     aria-hidden="true">
                    <path fill-rule="evenodd"
                          d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
                </svg>
            </a>示例4，URI重写
        </h1>
        <p>
            <code>gf</code>框架的静态文件服务支持将任意的<code>URI</code>重写，替换为制定的<code>URI</code>，使用<code>SetRewrite/SetRewriteMap</code>方法。
        </p>
        <p>示例，在<code>/Users/john/Temp</code>目录下只有两个文件<code>test1.html</code>及<code>test2.html</code>。</p>
        <div class="highlight highlight-source-go"><pre><span class="pl-k">package</span> main

<span class="pl-k">import</span> <span class="pl-s">"github.com/gogf/gf/frame/g"</span>

<span class="pl-k">func</span> <span class="pl-en">main</span>() {
    <span class="pl-s1">s</span> <span class="pl-c1">:=</span> <span class="pl-s1">g</span>.<span
                        class="pl-en">Server</span>()
    <span class="pl-s1">s</span>.<span class="pl-en">SetServerRoot</span>(<span class="pl-s">"/Users/john/Temp"</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">SetRewrite</span>(<span class="pl-s">"/test.html"</span>, <span
                        class="pl-s">"/test1.html"</span>)
        <span class="pl-s1">s</span>.<span class="pl-en">SetRewriteMap</span>(g.<span class="pl-smi">MapStrStr</span>{
            <span class="pl-s">"/my-test1"</span> : <span class="pl-s">"/test1.html"</span>,
            <span class="pl-s">"/my-test2"</span> : <span class="pl-s">"/test2.html"</span>,
        })
    <span class="pl-s1">s</span>.<span class="pl-en">SetPort</span>(<span class="pl-c1">8199</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">Run</span>()
}</pre>
        </div>
        <p>执行后，</p>
        <ol>
            <li>当我们访问 <code>/test.html</code> ，其实最终被重写到了 <code>test1.html</code>，返回的是该文件内容；</li>
            <li>当我们访问 <code>/my-test1</code> ，其实最终被重写到了 <code>test1.html</code>，返回的是该文件内容；</li>
            <li>当我们访问 <code>/my-test2</code> ，其实最终被重写到了 <code>test2.html</code>，返回的是该文件内容；</li>
        </ol>
    </article>
</div>